package com.pets.controller;


import com.pets.entity.LanguageType;
import com.pets.entity.Users;
import com.pets.service.ILanguageTypeService;
import com.pets.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 语言标签类型 前端控制器
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-11
 */
@RestController
@Api(tags = "语言标签类型接口")
@RequestMapping("/language-type")
public class LanguageTypeController {

    @Autowired
    private ILanguageTypeService languageTypeService;

    @ApiOperation("创建语言标签（只需要传标签名称->languageTypeName）")
    @PostMapping
    public Result register(@RequestBody LanguageType languageType) {
        if(languageType.getLanguageTypeName()==null||languageType.getLanguageTypeName().isEmpty()){
            return Result.fail("未设置标签名称！");
        }
        languageType.setCreateTime(LocalDateTime.now());
        return languageTypeService.register(languageType);
    }

    @ApiOperation("根据id标签")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id,HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            if (!user.getIsAdministrators()){
                return Result.fail("您不是管理员！");
            }
        }else {
            return Result.fail("未登录！");
        }*/
        return languageTypeService.deleteById(id);
    }


    @ApiOperation("修改标签（只能修改languageTypeName字段）")
    @PutMapping("/update")
    public Result update(@RequestBody LanguageType languageType,HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            if (!user.getIsAdministrators()){
                return Result.fail("您不是管理员！");
            }
        }else {
            return Result.fail("未登录！");
        }*/
        return languageTypeService.updateData(languageType);
    }

    @ApiOperation("查询标签集合")
    @GetMapping("/list")
    public Result listPage(@ApiParam(value = "标签名称")@RequestParam(required = false) String languageTypeName) {
        return languageTypeService.selectList(languageTypeName);
    }
}
